import {requestApi, type ResponseBody} from "./api.ts";
import CryptoJS from "crypto-js";

export function login(user: { username: string, password: string }): ResponseBody<boolean> {
    // 使用sha-256加密
    user.password = CryptoJS.SHA256(user.password).toString()
    return requestApi.post('/user/login', user)
}

export function logout(): ResponseBody<boolean> {
    return requestApi.put('/user/logout')
}

export interface UserInfo {
    id: number,
    username: string,
    email: string,
    passwordHash: string,
    role: string,
    avatar: string,
    status: string,
    createAt: Date,
    updatedAt: Date
}


export function getUserInfo(username: string): ResponseBody<UserInfo> {
    const formData = new FormData();
    formData.append("username", username);
    return requestApi.post('/user/userInfo', formData)
}

export function updateUserInfo(user: UserInfo) {
    return requestApi.put('/user/update', user)
}